home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / xref.c < prev   
C/C++ Source or Header  |  1997-12-07  |  23KB  |  874 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/xref.c 1.15 1997/05/18 10:05:52 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4.
  5.  
  6.   Cross referencing of functions.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96,97 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ The names of the function cross reference files. +*/
  17. #define XREF_FUNC_FILE   ".function"
  18. #define XREF_FUNC_BACKUP ".function~"
  19.  
  20. /*+ The names of the variable cross reference files. +*/
  21. #define XREF_VAR_FILE    ".variable"
  22. #define XREF_VAR_BACKUP  ".variable~"
  23.  
  24. /*+ The names of the include cross reference files. +*/
  25. #define XREF_INC_FILE    ".include"
  26. #define XREF_INC_BACKUP  ".include~"
  27.  
  28. /*+ The names of the type cross reference files. +*/
  29. #define XREF_TYPE_FILE   ".typedef"
  30. #define XREF_TYPE_BACKUP ".typedef~"
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36.  
  37. #ifdef AMIGA /* olsen */
  38. #include "amiga.h"
  39. #endif /* AMIGA */
  40.  
  41. #include "memory.h"
  42. #include "datatype.h"
  43. #include "cxref.h"
  44.  
  45. /*+ The name of the directory for the output. +*/
  46. extern char* option_odir;
  47.  
  48. /*+ The base name of the file for the output. +*/
  49. extern char* option_name;
  50.  
  51. /*+ The option for cross referencing. +*/
  52. extern int option_xref;
  53.  
  54. /*+ The option for indexing. +*/
  55. extern int option_index;
  56.  
  57. static void check_for_called(File file,char* called,char* caller,char* filename);
  58. static void check_for_caller(File file,char* called,char* filename);
  59. static void check_for_var(File file,char* variable,char* filename,int scope,char* funcname);
  60. static int  check_for_var_func(File file,Variable var,Function func);
  61. static void fixup_extern_var(Variable var,StringList2 refs);
  62.  
  63. /*++++++++++++++++++++++++++++++++++++++
  64.   Cross reference the functions, variables and includes that are used in this file
  65.   with the global functions, variables and includes. The types that are defined are also listed here.
  66.  
  67.   File file The file structure containing the information.
  68.   ++++++++++++++++++++++++++++++++++++++*/
  69.  
  70. void CrossReference(File file)
  71. {
  72.  FILE *in,*out;
  73.  char *ifile,*ofile;
  74.  
  75.  /* Format: filename [[%]include1] [[%]include2] ... : Files include1, include2, ... are included in filename;
  76.     those with a % are local. */
  77.  
  78.  if(option_xref&XREF_FILE) /* First do the files */
  79.    {
  80.     Include inc;
  81.  
  82.     ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_INC_FILE);
  83.     ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_INC_BACKUP);
  84.  
  85.     in =fopen(ifile,"r");
  86.     out=fopen(ofile,"w");
  87.  
  88.     if(!out)
  89.       {fprintf(stderr,"cxref: Failed to open the include cross reference file '%s'\n",ofile);exit(1);}
  90.  
  91.     fprintf(out,"%s",file->name);
  92.     for(inc=file->includes;inc;inc=inc->next)
  93.        fprintf(out," %s%s",inc->scope==LOCAL?"%":"",inc->name);
  94.     fprintf(out,"\n");
  95.  
  96.     if(in)
  97.       {
  98.        char include[128],filename[128],ch;
  99.  
  100.        while(fscanf(in,"%s%c",filename,&ch)==2)
  101.          {
  102.           int diff_file=strcmp(filename,file->name);
  103.  
  104.           if(diff_file)
  105.              fprintf(out,"%s",filename);
  106.  
  107.           while(ch==' ')
  108.             {
  109.              fscanf(in,"%s%c",include,&ch);
  110.  
  111.              if(diff_file)
  112.                 fprintf(out," %s",include);
  113.  
  114.              if(include[0]=='%' && !strcmp(&include[1],file->name))
  115.                 AddToStringList(file->inc_in,filename,1,1);
  116.             }
  117.  
  118.           if(diff_file)
  119.              fprintf(out,"\n");
  120.          }
  121.  
  122.        fclose(in);
  123.        unlink(ifile);
  124.       }
  125.  
  126.     fclose(out);
  127.     rename(ofile,ifile);
  128.    }
  129.  
  130.  /* Format: filename funcname scope [[%][&]funcname1] [[%][&]funcname2] ... : The function funcname in file filename
  131.     calls or references functions funcname1, funcname2 ... ; those with a % are local, with a & are references. */
  132.  /* Format: filename $ 0 [[%]&funcname1] [[%]&funcname2] ... : The file references functions funcname1, funcname2 ... ;
  133.     those with a % are local.  */
  134.  
  135.  if(option_xref&XREF_FUNC) /* Now do the functions */
  136.    {
  137.     Function func;
  138.     int i;
  139.  
  140.     ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_FUNC_FILE);
  141.     ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_FUNC_BACKUP);
  142.  
  143.     in =fopen(ifile,"r");
  144.     out=fopen(ofile,"w");
  145.  
  146.     if(!out)
  147.       {fprintf(stderr,"cxref: Failed to open the functional cross reference file '%s'\n",ofile);exit(1);}
  148.  
  149.     for(i=0;i<file->f_refs->n;i++)
  150.        check_for_called(file,ConcatStrings(2,"&",file->f_refs->s1[i]),NULL,file->name);
  151.  
  152.     for(func=file->functions;func;func=func->next)
  153.       {
  154.        for(i=0;i<func->calls->n;i++)
  155.           check_for_called(file,func->calls->s1[i],func->name,file->name);
  156.        for(i=0;i<func->f_refs->n;i++)
  157.           check_for_called(file,ConcatStrings(2,"&",func->f_refs->s1[i]),func->name,file->name);
  158.       }
  159.  
  160.     for(func=file->functions;func;func=func->next)
  161.        check_for_caller(file,func->name,file->name);
  162.  
  163.     if(file->f_refs->n)
  164.       {
  165.        fprintf(out,"%s $ 0",file->name);
  166.        for(i=0;i<file->f_refs->n;i++)
  167.          {
  168.           if(file->f_refs->s2[i])
  169.              fprintf(out," %%&%s",file->f_refs->s1[i]);
  170.           else
  171.              fprintf(out," &%s",file->f_refs->s1[i]);
  172.          }
  173.        fprintf(out,"\n");
  174.       }
  175.  
  176.     for(func=file->functions;func;func=func->next)
  177.       {
  178.        fprintf(out,"%s %s %d",file->name,func->name,func->scope);
  179.        for(i=0;i<func->calls->n;i++)
  180.          {
  181.           if(func->calls->s2[i])
  182.              fprintf(out," %%%s",func->calls->s1[i]);
  183.           else
  184.              fprintf(out," %s",func->calls->s1[i]);
  185.          }
  186.        for(i=0;i<func->f_refs->n;i++)
  187.          {
  188.           if(func->f_refs->s2[i])
  189.              fprintf(out," %%&%s",func->f_refs->s1[i]);
  190.           else
  191.              fprintf(out," &%s",func->f_refs->s1[i]);
  192.          }
  193.        fprintf(out,"\n");
  194.       }
  195.  
  196.     if(in)
  197.       {
  198.        char ch,funcname[64],filename[128],called[64];
  199.        int scope;
  200.  
  201.        while(fscanf(in,"%s %s %d%c",filename,funcname,&scope,&ch)==4)
  202.          {
  203.           int diff_file=strcmp(filename,file->name);
  204.  
  205.           if(diff_file)
  206.             {
  207.              if(funcname[0]!='$')
  208.                 check_for_caller(file,funcname,filename);
  209.              fprintf(out,"%s %s %d",filename,funcname,scope);
  210.             }
  211.  
  212.           while(ch==' ')
  213.             {
  214.              fscanf(in,"%s%c",called,&ch);
  215.              if(diff_file)
  216.                {
  217.                 if(called[0]!='%')
  218.                    check_for_called(file,called,funcname[0]=='$'?NULL:funcname,filename);
  219.                 fprintf(out," %s",called);
  220.                }
  221.             }
  222.  
  223.           if(diff_file)
  224.              fprintf(out,"\n");
  225.          }
  226.  
  227.        fclose(in);
  228.        unlink(ifile);
  229.       }
  230.  
  231.     fclose(out);
  232.     rename(ofile,ifile);
  233.    }
  234.  
  235.  /* Format: filename varname scope [$] [[%]funcname1] [[%]funcname2] ... : variable varname is used in
  236.     the file filename if $, and functions funcname1, funcname2 ... Those with a % are local.  */
  237.  
  238.  if(option_xref&XREF_VAR) /* Now do the variables */
  239.    {
  240.     Variable var;
  241.     Function func;
  242.  
  243.     ifile=ConcatStrings(4,option_odir,"/",option_name,XREF_VAR_FILE);
  244.     ofile=ConcatStrings(4,option_odir,"/",option_name,XREF_VAR_BACKUP);
  245.  
  246.     in =fopen(ifile,"r");
  247.     out=fopen(ofile,"w");
  248.  
  249.     if(!out)
  250.       {fprintf(stderr,"cxref: Failed to open the variable cross reference file '%s'\n",ofile);exit(1);}
  251.  
  252.     for(var=file->variables;var;var=var->next)
  253.       {
  254.        check_for_var(file,var->name,file->name,var->scope,NULL);
  255.        fprintf(out,"%s %s %d",file->name,var->name,var->scope);
  256.        if(check_for_var_func(file,var,NULL))
  257.           fprintf(out," $");
  258.        for(func=file->functions;func;func=func->next)
  259.           if(check_for_var_func(file,var,func))
  260.              fprintf(out," %s%s",func->scope==LOCAL?"%":"",func->name);
  261.        fprintf(out,"\n");
  262.       }
  263.  
  264.     if(in)
  265.       {
  266.        char varname[64],filename[128],funcname[64],ch;
  267.        int scope;
  268.  
  269.        while(fscanf(in,"%s %s %d%c",filename,varname,&scope,&ch)==4)
  270.